<?php
   global $objTemplate, $arrConfig,$sCanEdit,$lang,$objResponse;
   include('../modules/testtab.php');
   $objXajax->registerFunction("ajxLoadProvince"); 
   $objXajax->registerFunction("ajxLoadDataWard");
   $objXajax->registerFunction("ajxLoadDataProvince");
   $objXajax->registerFunction("ajxLoadDataCity");
   //$objXajax->registerFunction("callalert");  
  $sCanEdit = 1;  
  initPage();
  function  initPage(){ 
  global $cmbCity1,$cmbCity2,$cmbProvince,$bodygrid,$objTemplate,$objDbSelect ; 
      $cmbCity1 = createCombobox('tbl_city',array('id','city_name'),array('name'=>'city_name','value'=>'id'),'cmbCity1',false);
      $cmbCity2 = createCombobox('tbl_city',array('id','city_name'),array('name'=>'city_name','value'=>'id'),'cmbCity2',false);
      $cmbProvince = createCombobox('tbl_province',array('id','city_name'),array('name'=>'province_name','value'=>'id'),'cmbProvine',false,'WHERE 1=0');
       $arrdata = getCityInfo();
       $bodygrid ='';
       for($i=0;$i<count($arrdata);$i++){
           $name = $arrdata[$i]['city_name'];
           $lat = $arrdata[$i]['latcenter'];
           $lng = $arrdata[$i]['longcenter'];
           $bodygrid .= "<tr>
            <td>$name</td>
            <td>$lat</td>
            <td>$lng</td>
            <td>$name</td>
        </tr>";
       }
  }
  function getCityInfo(){
      $arr = array('city_name','latcenter','longcenter');
      return SelectDB('tbl_city',$arr);
  }
  function getProvinceByCity($city_id){ 
  global $objDbSelect;
       $sql = 'SELECT city_name,province_name,latcenter,longcenter FROM  tbl_province as p join 
  tbl_city as c on p.city_id = c.id WHERE p.city_id ='.$city_id;  
  $arr = $objDbSelect->GetArray($sql);   
      return $arr;      
  }
  function getWardByProvince($province_id){
      
  global $objDbSelect;
        $sql = 'SELECT city_name,province_name,ward_name,latcenter,longcenter FROM  tbl_ward as w join tbl_province as p on w.province_id = p.id join tbl_city as c on p.city_id = c.id WHERE w.province_id ='.$province_id;  
  $arr = $objDbSelect->GetArray($sql);   
  return $arr;   
  }
   function ajxLoadProvince($city){
     $objResponse = new xajaxResponse();
    $cmbProvince = createCombobox1($city);
    $objResponse->addAssign('tdProvince','innerHTML',"$cmbProvince");
    return $objResponse->getXML();     
 }
function createCombobox1($city){
    global $objDbSelect;
    $strResult ="<select id='cmbProvince1' name='cmbProvince1' onchange='loadWard(this.value)'>";
    $strResult.="<option value='-1'>".SYSTEM_MUST_SELECT."</option>"; 
    $sql = 'select * from tbl_province where city_id='.$city;
    $arrValue = $objDbSelect->GetArray($sql);  
      $count = count($arrValue);
      for($i=0;$i<$count;$i++){
          $valuetemp = $arrValue[$i]['id'];
          $nametemp = $arrValue[$i]['province_name'];
          $strResult.="<option value='$valuetemp'>$nametemp</option>"; 
      }
      $strResult.="</select>";
      return $strResult;    
}
  
function ajxLoadDataCity()
{     
    global $lang,$objDbSelect;
    $lang ='vi';
    $arrCity = GetCity();
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/main_city_grid.html');
    $oTbs->MergeBlock('blk_data', $arrCity);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('divGrid', 'innerHTML', $oTbs->Source); 
    $objResponse->addScriptCall('initTable();');                              
    return $objResponse->getXML();
}



function ajxLoadDataProvince($arrParams=array())
{     
    global $lang,$objDbSelect;
    $lang ='vi';
    $arrProvince = GetProvince($arrParams);
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/main_province_grid.html');
    $oTbs->MergeBlock('blk_data', $arrProvince);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('divGrid', 'innerHTML', $oTbs->Source); 
    $objResponse->addScriptCall('initTable();');                              
    return $objResponse->getXML();
}


function ajxLoadDataWard($arrParams=array())
{     
    global $lang,$objDbSelect;
    $lang ='vi';
    $arrCity = GetWard($arrParams);
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/main_ward_grid.html');
    $oTbs->MergeBlock('blk_data', $arrCity);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('divGrid', 'innerHTML', $oTbs->Source); 
    $objResponse->addScriptCall('initTable();');                              
    return $objResponse->getXML();
}

function GetCity($id=''){
 global $objDbSelect;
 $condition = '';
 if($id!=''){
     $id = addslashes($id);
   $condition= " WHERE id =   '$id'";
 }
 $sql = "select * from tbl_city $condition";
 return $objDbSelect->GetArray($sql);    
}
function GetProvince($arrParams=array()){
 global $objDbSelect;
 $condition = ' WHERE 1 = 1 ';
 if(isset($arrParams['city_id'])&&$arrParams['city_id']!=''){
     $city_id = addslashes($arrParams['city_id']);
     $condition.= " and city_id = '$city_id' ";
 }
 if(isset($arrParams['province_id'])&&$arrParams['province_id']!=''){
     $province_id = addslashes($arrParams['province_id']);
     $condition.= " and id = '$province_id' ";
 }

 $sql = "select pr.*,city_name from tbl_province pr join tbl_city ct on ct.id = pr.city_id $condition" ;
 return $objDbSelect->GetArray($sql);    
}

function GetWard($arrParams=array()){
 global $objDbSelect;
 $condition = ' WHERE 1 = 1 ';
 if(isset($arrParams['city_id'])&&$arrParams['city_id']!=''){
     $city_id = addslashes($arrParams['city_id']);
     $condition.= " and wd.city_id = '$city_id' ";
 }
 if(isset($arrParams['province_id'])&&$arrParams['province_id']!=''){
     $province_id = addslashes($arrParams['province_id']);
     $condition.= " and province_id = '$province_id' ";
 }
 
 if(isset($arrParams['ward_id'])&&$arrParams['ward_id']!=''){
     $ward_id = addslashes($arrParams['ward_id']);
     $condition.= " and id = '$ward_id' ";
 }
 
 $sql = "select wd.*,province_name,city_name from tbl_ward wd join tbl_province pr on wd.province_id = pr.id join tbl_city ct on ct.id = wd.city_id $condition " ;
return $objDbSelect->GetArray($sql);    
}

?>
